
#include "IGlobal.h"
#include <BShortcut>
#include "member_BShortcut.h"

using namespace BWE;

#define member					(*(member_BShortcut*)_ptr)
#define member_allocate()		_ptr = new member_BShortcut(this)
#define member_release()		delete (member_BShortcut*)_ptr

BShortcut::BShortcut(BWidget* widget)
{
	member_allocate();
	member.widget = widget;
	app_shortcuts.append(this);
}
BShortcut::BShortcut(const BString& text, BWidget* widget)
{
	member_allocate();
	member.widget = widget;
	setText(text);
	app_shortcuts.append(this);
}
BShortcut::~BShortcut()
{
	app_shortcuts.remove(this);
	member_release();
}

void BShortcut::setHost(BAction* host)
{
	if (member.host != host)
	{
		member.host = host;
	}
}
BAction* BShortcut::host() const
{
	return member.host;
}

void BShortcut::setWidget(BWidget* widget)
{
	if (member.widget != widget)
	{
		member.widget = widget;
	}
}
BWidget* BShortcut::widget() const
{
	return member.widget;
}

void BShortcut::setValue(const BValue& value)
{
	if (member.value != value)
	{
		member.value = value;
		this->emit(Signal_ValueChanged, value);
	}
}
const BValue& BShortcut::value() const
{
	return member.value;
}

bool BShortcut::setText(const BString& text)
{
	if (member.text != text)
	{
		member.text = text;
		member.fromText(text);
		if (this->checks(Signal_TextChanged))
			this->emit(Signal_TextChanged, text);
		return true;
	}
	return false;
}
const BString& BShortcut::text() const
{
	return member.text;
}

bool BShortcut::setKeys(const BKeyArray& keys)
{
	if (member.keys != keys)
	{
		member.keys = keys;
		return true;
	}
	return false;
}
const BKeyArray& BShortcut::keys() const
{
	return member.keys;
}

bool BShortcut::clearKeys()
{
	if (member.keys.size())
	{
		member.keys.clear();
		return true;
	}
	return false;
}

void BShortcut::trigger()
{
	this->emit(Signal_Triggered, member.value);
	if (BAction* action = member.host)
	{
		action->trigger();
	}
}
